import { tableOption } from './index.config.js'
import addCategory from './../addCategory/index.vue'
import { shopgoodscategoryfetchList, delObj, addObj, putObj } from '@/api/shop/GoodsCategory.js'

export default {
    name: "GoodsCategory",
    components: { addCategory },
    data() {
        return {
            tableOption,
            loading: false,
            loadData: [],
            page: {
                current: 1,
                total: 0,
                size: 10
            },
            selectedRows: [],//选中的数组
            selectedRowKeys: [],  //选中的数组
            selectedRows: [],
            number: 0,
            btn: [
                {
                    title: "新增",
                    isShow: true
                },
            ],
            businessType: ''
        }
    },
    // created() {
    //     this.getList()
    // },
    methods: {
        getList(searchForm, page, key) {
            if (searchForm) {
                console.log(searchForm)
            }
            if (page) {
                this.page.current = page
            }
            if (key) {
                this.businessType = key
            }
            shopgoodscategoryfetchList(Object.assign({ businessType: this.businessType, deptId: this.$store.getters.userInfo.sysUser.deptId, tenantId: this.$store.getters.userInfo.sysUser.tenantId }, searchForm ? searchForm : {}, this.page)).then(val => {
                let data = val.data
                this.page = {
                    current: data.current,
                    total: data.total,
                    size: data.size,
                }
                const newData = [...data.records]
                newData.forEach(item => {
                    if (item.children.length) {
                        item.delFlag = true
                    } else {
                        item.delFlag = false
                    }
                    item.children.forEach(items => {
                        delete items.children
                    })
                })
                this.loadData = newData
                console.log(this.loadData)
            })
        },
        onShowSizeChange(current, size) {
            console.log(current, size)
            this.page.current = current
            this.page.size = size
            this.getList()
        },
        // 清空选中项
        clearSelection() {
            this.selectedRowKeys = []
            this.number = 0
        },
        handler(index) {
            if (index == 0) {
                this.addCategory()
            }
        },
        // 新增商超
        addCategory() {
            this.$refs.addCategory.dialogVisible = true
            this.$refs.addCategory.dialogVisibleTile = '新增'
            this.$refs.addCategory.businessType = this.businessType
            this.$refs.addCategory.getCategory({})
        },
        handlEdit(row) {
            this.$refs.addCategory.dialogVisible = true
            this.$refs.addCategory.dialogVisibleTile = '编辑'
            this.$refs.addCategory.businessType = this.businessType
            this.$refs.addCategory.getCategory(row)

        },
        handlDel(row) {
            var that = this

            this.$confirm({
                title: '确定要删除类目名称为' + row.categoryName + '吗？',
                onOk() {
                    delObj(row.id).then(val => {
                        that.getList({}, 1, that.businessType)
                        that.$message.success('删除成功')
                    })
                },
                class: 'test',
            });
        },
        switchChange(val, row) {
            let obj = {}
            obj = row
            if (val) {
                obj.showFlag = '01'
                putObj(obj).then(val => {
                    this.$message.success('前台显示成功')
                })
            } else {
                obj.showFlag = '02'
                putObj(obj).then(val => {
                    this.$message.success('前台隐藏成功')
                })
            }
            console.log(val)
        },
    }

} 